package internationalenergytradingplatform.controller.AdministratorsController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.VO.BuyerPageQueryVO;
import internationalenergytradingplatform.VO.OrderPageQueryVO;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.dao.BuyerMapper;
import internationalenergytradingplatform.dto.BuyerPageQueryDTO;
import internationalenergytradingplatform.dto.OrderPageQueryDTO;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/administrator-buyer")
public class AdministratorBuyerController {

    @Autowired
    private BuyerMapper buyerMapper;

    @Autowired
    private BuyerAccountDao buyerAccountDao;
    @GetMapping("/paginationQuery")
    public Result<PageResult> adminPagingToQueryAllOrder(BuyerPageQueryDTO buyerPageQueryDTO){

        PageHelper.startPage(buyerPageQueryDTO.getPage(), buyerPageQueryDTO.getPageSize());
        Page<BuyerPageQueryVO> page = buyerMapper.buyerPageQueryAdmin(buyerPageQueryDTO);
        long total = page.getTotal();
        List<BuyerPageQueryVO> records = page.getResult();
        log.info("管理员分页查询结果：{}", records);
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }

    @PutMapping("/updateBuyerAccountState")
    public Result<String> updateBuyerAccountState(String tele, Integer state) {
        int updatedCount = buyerAccountDao.updateBuyerAccountState(tele, state);
        if (updatedCount > 0) {
            return Result.success("采购商状态更新成功");
        } else {
            return Result.error("采购商状态更新失败");
        }
    }
}
